package com.example.controller;

import com.aliyun.oss.AliOSSUtils;
import com.example.msg.Result;
import com.example.utils.AliOSSUtils1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@Slf4j
public class UploadController {

    @Resource
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(String username, Integer age,
                         @RequestParam(value = "image") MultipartFile file) throws IOException {

        // 注意：springboot 默认文件上传允许的最大大小为1M 如果需要上传大文件 需要进行配置
        log.info(username, age, file);
       /*
        存本地

        // 原文件名称
        String originalFilename = file.getOriginalFilename();
        // 从头开始截取，直到最后一个点为止 拼接uuid防止文件名称一样覆盖
        String newFileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        // 磁盘目录
        String pathName = "D:\\BaiduNetdiskDownload\\day11-SpringBootWeb案例\\day11-SpringBootWeb案例\\";
        File file1 = new File(pathName + newFileName);
        // 存本地
        file.transferTo(file1); */

        String url = aliOSSUtils.upload(file);

        return Result.success(url);
    }
}
